package com.oss.service.oss;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oss.dto.oss.investmentadvice.UpdateDto;
import com.oss.entity.oss.InvestmentAdvice;
import com.oss.entity.oss.InvestmentAdviceDetail;
import com.oss.enums.IsDeletedEnum;
import com.oss.mapper.oss.InvestmentAdviceDetailMapper;
import com.oss.util.SnowFlakeUtil;
import com.oss.vo.oss.investmentadvice.InvestmentAdviceDetailVO;
import com.oss.vo.oss.student.StudentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.TemporalUnit;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author chengqiang
 */
@Service
public class InvestmentAdviceDetailService extends ServiceImpl<InvestmentAdviceDetailMapper, InvestmentAdviceDetail> {
    @Autowired
    StudentService studentService;
    /**
     * 根据投资建议主ID查询详情
     * @param mids
     * @return key = mid , value=详情列表
     */
    public Map<String, List<InvestmentAdviceDetailVO>> getByMID(Set<String> mids){
        if (CollectionUtils.isNotEmpty(mids)) {
            LambdaQueryWrapper<InvestmentAdviceDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(InvestmentAdviceDetail::getIsDeleted, IsDeletedEnum.NO.getFlag());
            wrapper.in(InvestmentAdviceDetail::getMid, mids);
            List<InvestmentAdviceDetail> list =  this.list(wrapper);
            if (CollectionUtils.isNotEmpty(list)) {
                Set<String> studentIds = list.stream().map(InvestmentAdviceDetail::getStudentID).collect(Collectors.toSet());
                Map<String, StudentVO> studentMap = studentService.selectByIDs(studentIds);
                List<InvestmentAdviceDetailVO> voList = list.stream().map(entity->{
                    InvestmentAdviceDetailVO vo = new InvestmentAdviceDetailVO();
                    BeanUtils.copyProperties(entity,vo);
                    if (studentMap.containsKey(entity.getStudentID())) {
                        StudentVO studentVO = studentMap.get(entity.getStudentID());
                        vo.setStudentID(studentVO.getId());
                        vo.setStudentName(studentVO.getName());
                        vo.setStudentAccount(studentVO.getAccount());
                        vo.setStudentPhone(studentVO.getPhone());
                    }
                    return vo;
                }).collect(Collectors.toList());
                return voList.stream().collect(Collectors.groupingBy(InvestmentAdviceDetailVO::getMid));
            }
        }
        return new HashMap<>();
    }
    public void add(String mid,Set<String> studentIdSet){
        if (CollectionUtils.isNotEmpty(studentIdSet)){
            List<InvestmentAdviceDetail> addList = new ArrayList<>();
            for (String studentId : studentIdSet){
                InvestmentAdviceDetail entity = new InvestmentAdviceDetail();
                entity.setMid(mid);
                entity.setId(SnowFlakeUtil.getID());
                entity.setStudentID(studentId);
                entity.setStatus(0);
                addList.add(entity);
            }
            this.saveBatch(addList);
        }
    }

    public void approval(UpdateDto dto) {
        LambdaUpdateWrapper<InvestmentAdviceDetail> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(InvestmentAdviceDetail::getId,dto.getIds());
        if (StringUtils.isNotBlank(dto.getRemark())) {
            lambdaUpdateWrapper.set(StringUtils.isNotBlank(dto.getRemark()),InvestmentAdviceDetail::getRemark,dto.getRemark());
        }
        lambdaUpdateWrapper.set(InvestmentAdviceDetail::getStatus,dto.getStatus());
        this.update(null,lambdaUpdateWrapper);
    }

    /**
     * 查询某个学员2天内的投资建议信息
     * @param studentID
     * @return
     */
    public List<InvestmentAdviceDetail> selectByStudentID(String studentID) {
        LambdaQueryWrapper<InvestmentAdviceDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InvestmentAdviceDetail::getIsDeleted, IsDeletedEnum.NO.getFlag());
        wrapper.eq(InvestmentAdviceDetail::getStudentID, studentID);
        wrapper.ge(InvestmentAdviceDetail::getStatus, 1); // 状态 >=1
        wrapper.ge(InvestmentAdviceDetail::getCreateTime, LocalDateTime.now().minusDays(2));
        return this.list(wrapper);
    }
}
